package guestbook

import (
	"github.com/labstack/echo"
	auth "tacitus/library/authorized"
	errs "tacitus/library/error"
	resp "tacitus/library/response"
	"time"
)

func ShowCommentListHandler(c echo.Context) error {
	i := new(resp.PageFields)
	if err := c.Bind(i); err != nil {
		return err
	}
	data := make([]Guestbook, 0)
	count, err := x.OrderBy("create_time DESC").Limit(i.Size(), i.Offset()).FindAndCount(&data)
	if err != nil {
		return err
	}
	return resp.Page(c, data, *i.SetItemCount(int(count)))
}

func ReplyCommentHandler(c echo.Context) error {
	uid := auth.CurrentUser(c).UserID
	i := new(Guestbook)
	if err := c.Bind(i); err != nil {
		return err
	}
	co := new(Guestbook)
	if ok, err := x.Where("id = ?", i.ID).Get(co); err != nil {
		return err
	} else if !ok {
		return errs.Called("指定的留言不存在")
	}
	co.ReplyUserID = uid
	co.ReplyContent = i.ReplyContent
	co.ReplyTime = time.Now().Unix()
	co.Publicity = i.Publicity
	if _, err := x.Where("id = ?", i.ID).Update(co); err != nil {
		return err
	}
	return nil
}